import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 1. 自傲准输入、输出流
 * System.in
 * System.out
 * 通过调用如下的方法，修改输入流和输出流的位置
 * setIn(InputStream is)
 * setOut(OutputStream os)
 */
public class OtherStreamTest {
    /**
     * 从键盘读取字符串，要求将读取到的整行字符串转成大写输出
     * 然后继续进行输出操作，直至输入 e / exit，退出程序。
     */
    public static void main(String[] args) {
        System.out.println("请输入信息(退出输入e或exit):");
        InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String s;
        try {
            while ((s = bufferedReader.readLine()) != null) {
                if (s.equalsIgnoreCase("e") || s.equalsIgnoreCase("exit")) {
                    bufferedReader.close();
                    System.out.println("退出成功！");
                    break;
                }
                System.out.println(s.toUpperCase());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test() {
        try (FileOutputStream fileOutputStream = new FileOutputStream("write.txt");
             // 创建打印输出流，设置为自动刷新模式（写入换行符或字节'\n'时，都会刷新输出缓存区）
             PrintStream ps = new PrintStream(fileOutputStream, true)) {
            ps.println("OK");

            System.setOut(ps);
            System.out.println("\nAAA");
            System.out.println(Person.alive);
            for (int i = 0; i < 255; i++) {
                System.out.print((char) i + "\t");
                if (i % 10 == 0)
                    System.out.println();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 记录日志的方法
     */
    public static void log(String msg) {
        try (PrintStream printStream = new PrintStream(new FileOutputStream("log.txt", true))) {
            System.setOut(printStream);
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
            String dateTime = simpleDateFormat.format(date);

            System.out.println(dateTime + ": " + msg);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    @Test
    public void testLog() {
        log("调用了Xxx方法");
        log("验证失败。");
        String s = "sda";

    }

    @Test
    public void testFileUtil() throws IOException {
        FileUtils.copyFile(new File("期末总结-GBK.txt"), new File("期末总结-GBK-copy.txt"));

    }
}
